import isWindow from './isWindow';

export type Position = {
  left: number;
  top: number;
};

export type Target = HTMLElement | Document | Window;

export function getScroll(target: Target): Position {
  let left: number;
  let top: number;
  if (!target) {
    return { left: 0, top: 0 };
  }
  if (isWindow(target)) {
    left = (target as Window).screenX;
    top = (target as Window).screenY;
  } else if (target instanceof Document) {
    left = target.documentElement.scrollLeft;
    top = target.documentElement.scrollTop;
  } else {
    left = (target as HTMLElement).scrollLeft;
    top = (target as HTMLElement).scrollTop;
  }
  return { left, top };
}
